package raceReservationServer.raceServerGateway;

import java.util.LinkedList;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.*;

import JMSRaceServer.RaceJMSDTO;



import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class JMSRaceServerGateway implements IRaceServerGateway {
	
    private Context                 jndiContext = null;
    private QueueConnectionFactory  queueConnectionFactory = null;
    private QueueConnection         queueConnection = null;
    private QueueSession            queueSession = null;
    private Queue                   queue = null;
    private QueueReceiver           queueReceiver = null;
    private ObjectMessage           objectMessage = null;
	
	public JMSRaceServerGateway(String queueName){
		queueName = new String(queueName);
        System.out.println("Queue name is " + queueName);
        
        try {
           	Properties props = new Properties();
        	props.load(new FileInputStream("jndi.properties"));
            jndiContext = new InitialContext(props);
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API context: " + e.toString());
            System.exit(1);
        }catch(IOException ioe){
            System.out.println("Could not find properties file: " + ioe.toString());
            System.exit(1);        	
        }

        try {
        	queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
        	queue = (Queue) jndiContext.lookup(queueName);        	
        } catch (NamingException e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            System.exit(1);
        }
	}
	

	public List<RaceDO> getRaces() {
		List<RaceDO> result = new LinkedList<RaceDO>();    
		List<RaceJMSDTO> aux= new LinkedList<RaceJMSDTO>();
		RaceDO raceDO=null;
        try {
            queueConnection = queueConnectionFactory.createQueueConnection();
            queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            queueReceiver = queueSession.createReceiver(queue);
            queueConnection.start();
            while (true) {
            	Message m = queueReceiver.receive(1);
            	if (m != null) {
            		if (m instanceof ObjectMessage){
	            		objectMessage = (ObjectMessage) m;
	            		System.out.println("Después del cast del object");
	            		RaceJMSDTO race = (RaceJMSDTO) objectMessage.getObject();
	            		aux.add(race);
	            		System.out.println("Reading message: " + race.getName());
            		}
            		else System.out.println ("No es");
            	}else{
            		break;
            	}
            }
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (queueConnection != null) {
                try {
                	System.out.println("Closing the queueconnection.");
                    queueConnection.close();
                	System.out.println("queue closed.");
                } catch (JMSException e) {}
            }
        }
        System.out.println("Before the for");
        for (int i=0; i<aux.size(); i++){
        	result.add(new RaceDO(aux.get(i).getName(), aux.get(i).getDistance(), aux.get(i).getDate(), aux.get(i).getaAvailablePlaces()));     	
        }
        System.out.println("Before the return");
		return result;
	}
}
